﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 25/05/2009
// Time: 19:45
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.security
{
	/// <summary>
	/// Description of Protection.
	/// </summary>
	public class Protection
	{
		string systemName;
		string fileName = Application.StartupPath+"\\license.txt";
		
		public Protection()
		{
			systemName =Application.StartupPath;
			systemName = systemName.Substring(systemName.LastIndexOf("\\")+1);
		}
		
		[DllImport("activation.dll")]
   	public static extern String getCpuId();
   	
   	[DllImport("activation.dll")]
   	public static extern String validateProtection(String fileName, String systemName);
   	
   	[DllImport("activation.dll")]
   	public static extern bool createTrialProtection(String fileName, String systemName, int numberDays);
   	
   	[DllImport("activation.dll")]
   	public static extern String getParam(String fileName, String systemName, String param);
   	
   	[DllImport("activation.dll")]
   	public static extern bool setParam(String fileName, String systemName, String param, String value);
   	
   	[DllImport("activation.dll")]
   	public static extern String setParamEncrypted(String fileName, String systemName, String param, String value);
   	
   	[DllImport("activation.dll")]
   	public static extern String getExpirationDate(String fileName, String systemName);	
   	
   	public bool validateSystem(bool showMessageExpirationInFewDays, int dayNumbers)
   	{
   		string result;
   		DateTime today, aux;
   		int cont;
   		
			if (File.Exists(fileName))
			{
				string customer= getParam(fileName, systemName, Constant.paramCustomer);
				string serie= getParam(fileName, systemName, Constant.paramSeries);
				string key= getParam(fileName, systemName, Constant.paramKey);
				string cpuid= getParam(fileName, systemName, Constant.paramCpuId);
				string system= getParam(fileName, systemName, Constant.paramSystem);
				
				result = validateProtection(fileName, systemName);

				if (result.Equals(common.CONSTANTS.protectionOk))
				{
					if (showMessageExpirationInFewDays)
					{
						result = getExpirationDate(fileName, systemName);
						aux = Convert.ToDateTime(result);
						today= DateTime.Now;
						
						cont = aux.Subtract(today).Days;
						if (cont <= dayNumbers)
						{
							KryptonMessageBox.Show("Chave expira em "+cont+" dias, contacte o suporte!");
							csharp.common.Util.execFile(Application.StartupPath+"\\activations.exe","", false, false);	
						}
					}
					return true;
				}
				else if (result.Equals(common.CONSTANTS.protectionError))
				{
					KryptonMessageBox.Show("Erro de proteção, contacte o suporte!");
					csharp.common.Util.execFile(Application.StartupPath+"\\activations.exe","", false, false);	
					return false;
				}
				else if (result.Equals(common.CONSTANTS.protectionExpired))
				{
					KryptonMessageBox.Show("Chave de licença expirada, contacte o suporte!");
					csharp.common.Util.execFile(Application.StartupPath+"\\activations.exe","", false, false);	
					return false;
				}
				else if (result.Equals(common.CONSTANTS.protectionExpireToday))
				{
					KryptonMessageBox.Show("Chave de licença expira hoje, contacte o suporte!");
					csharp.common.Util.execFile(Application.StartupPath+"\\activations.exe","", false, false);	
					return true;
				}
				else if (result.Equals(common.CONSTANTS.protectionCpuIdError))
				{
					KryptonMessageBox.Show("Erro de proteção 1240, contacte o suporte!");
					csharp.common.Util.execFile(Application.StartupPath+"\\activations.exe","", false, false);	
					return false;
				}
				else 
				{
					KryptonMessageBox.Show("Erro de proteção indefinido, contacte o suporte!");
					csharp.common.Util.execFile(Application.StartupPath+"\\activations.exe","", false, false);	
					return false;				
				}
				
				/*if (getParam(fileName, systemName, Constant.paramCpuId) != getCpuId())
				{
					KryptonMessageBox.Show("Proteção inválida!");
					return false;
				}
				else
					return true;*/
			}
			else
				KryptonMessageBox.Show("Chave de licença inexistente!");
			return false;
   	}
	}
}
